package com.cp.mtq.exception;

import com.cp.mtq.bean.Result;
import com.cp.mtq.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class RestCtrlExceptionHandler {

    @ExceptionHandler(MtqException.class)
    @ResponseStatus(value = HttpStatus.OK)
    public Result<Object> handleXCloudException(MtqException e) {

        String errorMsg = "mtq exception";
        if (e!=null){
            errorMsg = e.getMsg();
            log.error(e.toString());
        }
        return new ResultUtil<>().setErrorMsg(500, errorMsg);
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(value = HttpStatus.OK)
    public Result<Object> handleException(Exception e) {

        String errorMsg = "Exception";
        if (e!=null){
            e.printStackTrace();
        }
        return new ResultUtil<>().setErrorMsg(500, errorMsg);
    }
}
